import cv2
import tensorflow as tf
from tensorflow import keras
import numpy as np
# 读取并处理图片
charList = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
def normalize_image_file(image_path):#归一化处理
    # 读取图像
    image = cv2.imread(image_path)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # 将图像归一化
    gray=cv2.resize(gray,(28,28))
    normalized_image = gray / 255.0
    return normalized_image

model = keras.models.load_model('CNN_model.h5')
image = normalize_image_file("A.jpg")#标准化图片和训练格式一样
#在处理图像时，一般使用四维的张量来表示，形状为（批次大小，高度，宽度，通道数）。
image = np.reshape(image, (1, 28, 28, 1))
predictions = model.predict(image)
max_probability_index = np.argmax(predictions)
max_probability = predictions[0][max_probability_index]
max_probability_label = charList[max_probability_index]
print("识别结果：{}，准确率：{}".format(max_probability_label,max_probability))